package com.mostromas.ess.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mostromas.ess.entities.PDDProductCat;
import com.mostromas.ess.project.mapper.PDDProductCatMapper;
import com.mostromas.ess.project.req.PDDProductCatREQ;
import com.mostromas.ess.project.service.IPDDProductCatService;
import com.mostromas.ess.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PDDProductCatServiceImpl extends ServiceImpl<PDDProductCatMapper, PDDProductCat> implements IPDDProductCatService {
    @Transactional
    @Override
    public Result saveList(List<PDDProductCat> list) {
        boolean state = this.saveOrUpdateBatch(list);
        return state?Result.ok():Result.error("插入错误");
    }

    @Override
    public Result queryList(PDDProductCatREQ req) {
        QueryWrapper<PDDProductCat> wrapper = new QueryWrapper<>();
        if(req.getLevel() != 0){
            wrapper.eq("level",req.getLevel());
        }
        if(req.getParentCatId() != 0){
            wrapper.eq("parent_cat_id",req.getParentCatId());
        }
        if(req.getCatId() != 0){
            wrapper.eq("cat_id",req.getCatId());
        }
        if(StringUtils.isNotEmpty(req.getParentPath())){
            wrapper.like("parent_path",req.getParentPath());
        }

        return Result.ok(baseMapper.selectList(wrapper));
    }
}
